import {Component, OnInit} from "@angular/core";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {ActivatedRoute} from "@angular/router";
import {Location} from "@angular/common";
import {SupplierService} from "../supplier.service";

@Component({
  selector: 'app-supplier-edit',
  templateUrl: './supplier-edit.component.html',
  styleUrls: ['./supplier-edit.component.css']
})
export class SupplierEditComponent implements OnInit {
  public isConfirmLoading: boolean = false;
  private code: string; //supplier
  public validateForm: FormGroup;//企业登录的表单

  constructor(private fb: FormBuilder, private supplierService: SupplierService, private route: ActivatedRoute, public location: Location) {
    this.validateForm = this.fb.group({
      name: [null, Validators.compose([Validators.required])], // 供应商名称 最长32个汉字 64个英文
      englishName: [null, Validators.compose([Validators.required])], // 英文名
      contacts: [null, Validators.compose([Validators.required])], // 联系人
      bank: [null, Validators.compose([Validators.required])], // 最长32个汉字 64个英文
      bankAccount: [null, Validators.compose([Validators.required])], // 开户行账户
      paymentTerm: [null, Validators.compose([Validators.required])], // 支付方式 
      creditRating: [null, Validators.compose([Validators.required])], // 信用等级
      supplierCategoryCode: [null, Validators.compose([Validators.required])], // 供应商类型编码
      website: [null, Validators.compose([Validators.required])], // 公司官网
      address: [null, Validators.compose([Validators.required])], // 地址 64个汉字，128个英文字符
      englishAddress: [null, Validators.compose([Validators.required])], // 英文地址
      email: [null, Validators.compose([Validators.required])], // 电子邮箱
      fax: [null, Validators.compose([Validators.required])], // 传真号
      mobile: [null, Validators.compose([Validators.required])], // 移动电话
      telephone: [null, Validators.compose([Validators.required])], // 固定电话
      zipCode: [null, Validators.compose([Validators.required])], // 邮编
      companyNature: [null, Validators.compose([Validators.required])], // 公司性质 国营 state_run ,民营privately operated，合资joint venture，外资 foreign capital
      summary: [null, Validators.compose([Validators.required])], // 备注 128 个汉字说明
    });
  }

  ngOnInit() {
    this.route.url.subscribe(url => {
      const curPath = url[0].path;
      if (curPath === 'modify') {
        this.code = this.route.snapshot.params.code;//获取参数
        this.loadSupplierInfo();
      }//修改前查询出供应商信息
    })
  }

  /**
   * 表单确认
   */
  addSupplier($event) {
    $event.preventDefault();
    //1.进行脏检查，提示未填的必填字段
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
    }
    if (this.validateForm.invalid) return;
    this.isConfirmLoading = true;
    let formData = Object.assign({},this.validateForm.value);
    //添加supplier
    if (this.code) {
      formData.code = this.code;
      this.supplierService.modifySupplier(formData).then((data: any) => {
        this.isConfirmLoading = false;
        this.validateForm.reset();
        this.location.back();
      }).catch(res => this.isConfirmLoading = false)
    } else {
      this.supplierService.addSupplier(formData).then((data: any) => {
        this.isConfirmLoading = false;
        this.location.back();
      }).catch(res => this.isConfirmLoading = false)
    }
  }

  /**
   * 查询supplier信息
   */
  loadSupplierInfo() {
    this.supplierService.loadSupplierByCode(this.code).then((data: any) => {
      this.validateForm.patchValue(data);
    })
  }


  getFormControl(name) {
    return this.validateForm.controls[name];
  }
}
